import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class JTextAreaDemo {
    JFrame frame = new JFrame("JTextArea Demo");
    JTextArea ta1,ta2;
    JButton copy,clear;

    public static void main(String args[]) {
        JTextAreaDemo tad = new JTextAreaDemo();
        tad.go();
    }

    public void go() {
        ta1 = new JTextArea(3,15);
        ta1.setSelectedTextColor(Color.red);
        ta2 = new JTextArea(7,20);
        JScrollPane jsp1 = new JScrollPane(ta1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        JScrollPane jsp2 = new JScrollPane(ta2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        copy = new JButton("copy");
        copy.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (ta1.getSelectedText() != null) {
                    ta2.append(ta1.getSelectedText() + "\n");
                } else {
                    ta2.append("\n" + ta1.getText() + "\n");
                }

            }
        });

        clear = new JButton("Clear");
        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ta2.setText("");
            }
        });

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        Border etched = BorderFactory.createEtchedBorder();
        Border border1 = BorderFactory.createTitledBorder(etched,"Input Area:");
        Border border2 = BorderFactory.createTitledBorder(etched,"Output Area:");
        panel1.setBorder(border1);
        panel2.setBorder(border2);
        panel1.add(jsp1);
        panel1.add(copy);
        panel2.add(jsp2);
        panel2.add(clear);

        Container cp = frame.getContentPane();
        cp.add(panel1,"Center");
        cp.add(panel2,"South");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
